use actix_files::{Files, NamedFile};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::web;

use crate::controller::{auth, sys};
use crate::controller::accounts::AccountsController;
use crate::controller::base::BaseController;
use crate::controller::dict::{DictController, DictGroupController};
use crate::controller::game_email::GameEmailController;
use crate::controller::game_role::GameRoleController;
use crate::controller::game_tool::GameToolController;
use crate::controller::health::health_check;
use crate::controller::index::{home, index};
use crate::controller::resource::ResourceController;
use crate::controller::role::RoleController;
use crate::controller::user::UserController;
use crate::controller::ws;

// 生成路由地址
pub fn general_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/health", web::get().to(health_check));
    ws_routes(cfg);
    role_routes(cfg);
    user_routes(cfg);
    resource_routes(cfg);
    auth_routes(cfg);
    dict_routes(cfg);
    sys_routes(cfg);
    accounts_routes(cfg);
    game_email_routes(cfg);
    game_role_routes(cfg);
    game_tool_routes(cfg);
    //默认打开静态资源的index.html
    cfg.service(index);
    //首页 home.html
    cfg.service(home);
    //挂载静态资源, 静态文件存放在相对路径的static目录内, 该项需要最后设置，因为挂载的访问地址是根路径/,在他之后的所有路由配置将会失效
    static_file_routes(cfg);
}

pub fn ws_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/ws", web::get().to(ws::index));
}
pub fn sys_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(sys::sys_version);
    cfg.service(sys::mode);
}

pub fn game_tool_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/un-auth-api/game_tool/get_token", web::get().to(GameToolController::get_token));
}

pub fn game_role_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/api/game_role/list", web::get().to(GameRoleController::list))
        .route("/api/game_role/page", web::post().to(GameRoleController::page))
        .route("/api/game_role/info/{id}", web::get().to(GameRoleController::info))
        .route("/api/game_role/update", web::post().to(GameRoleController::update))
        .route("/api/game_role/delete/{id}", web::get().to(GameRoleController::delete));
}


pub fn game_email_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/api/game_email/mail_page", web::post().to(GameEmailController::mail_page))
        .route("/api/game_email/remove_letter/{id}", web::get().to(GameEmailController::remove_letter))
        .route("/api/game_email/remove_postal/{id}", web::get().to(GameEmailController::remove_postal))
        .route("/api/game_email/send_mail", web::post().to(GameEmailController::send_mail));
}

pub fn accounts_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/api/accounts/list", web::get().to(AccountsController::list))
        .route("/api/accounts/page", web::post().to(AccountsController::page))
        .route("/api/accounts/info/{id}", web::get().to(AccountsController::info))
        .route("/api/accounts/add", web::post().to(AccountsController::add))
        .route("/api/accounts/update", web::post().to(AccountsController::update))
        .route("/api/accounts/delete/{id}", web::get().to(AccountsController::delete))
        .route("/api/accounts/reset_password", web::post().to(AccountsController::reset_password))
        .route("/api/accounts/recharge_bonds", web::get().to(AccountsController::recharge_bonds))
        .route("/api/accounts/reset_create_role/{id}", web::get().to(AccountsController::reset_create_role))
        .route("/api/accounts/set_max_role/{id}", web::get().to(AccountsController::set_max_role))
        .route("/api/accounts/open_dungeon/{id}", web::get().to(AccountsController::open_dungeon))
        .route("/un-auth-api/accounts/account_login", web::post().to(AccountsController::account_login));
}



pub fn dict_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/api/dict/list", web::get().to(DictController::list))
        .route("/api/dict/page", web::post().to(DictController::page))
        .route("/api/dict/info/{id}", web::get().to(DictController::info))
        .route("/api/dict/add", web::post().to(DictController::add))
        .route("/api/dict/update", web::post().to(DictController::update))
        .route(
            "/api/dict/delete/{id}",
            web::get().to(DictController::delete),
        )
        .route(
            "/api/dict_group/list",
            web::get().to(DictGroupController::list),
        )
        .route(
            "/api/dict_group/page",
            web::post().to(DictGroupController::page),
        )
        .route(
            "/api/dict_group/info/{id}",
            web::get().to(DictGroupController::info),
        )
        .route(
            "/api/dict_group/add",
            web::post().to(DictGroupController::add),
        )
        .route(
            "/api/dict_group/update",
            web::post().to(DictGroupController::update),
        )
        .route(
            "/api/dict_group/delete/{id}",
            web::get().to(DictGroupController::delete),
        )
        .route(
            "/api/dict_group/search_list",
            web::post().to(DictGroupController::search_list),
        )
        .route(
            "/un-auth-api/dict_group/get_dict_list_by_code/{code}",
            web::get().to(DictGroupController::get_dict_list_by_code),
        )
        .route(
            "/un-auth-api/dict_group/get_dict_list_by_id/{id}",
            web::get().to(DictGroupController::get_dict_list_by_id),
        );
}

pub fn user_routes(cfg: &mut web::ServiceConfig) {
    cfg.route(
        "/un-auth-api/user/list",
        web::get().to(UserController::list),
    )
    .route("/api/user/page", web::post().to(UserController::page))
    .route("/api/user/info/{id}", web::get().to(UserController::info))
    .route("/api/user/add", web::post().to(UserController::add))
    .route("/api/user/update", web::post().to(UserController::update))
    .route(
        "/api/user/delete/{id}",
        web::get().to(UserController::delete),
    )
    .route(
        "/api/user/reset_password",
        web::post().to(UserController::reset_password),
    )
    .route(
        "/api/user/get_role/{id}",
        web::get().to(UserController::get_role),
    )
    .route(
        "/api/user/set_role",
        web::post().to(UserController::set_role),
    );
}

pub fn role_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/api/role/list", web::get().to(RoleController::list))
        .route("/api/role/page", web::post().to(RoleController::page))
        .route("/api/role/info/{id}", web::get().to(RoleController::info))
        .route("/api/role/add", web::post().to(RoleController::add))
        .route("/api/role/update", web::post().to(RoleController::update))
        .route(
            "/api/role/delete/{id}",
            web::get().to(RoleController::delete),
        )
        .route(
            "/api/role/get_resource/{id}",
            web::get().to(RoleController::get_resource),
        )
        .route(
            "/api/role/set_resource",
            web::post().to(RoleController::set_resource),
        );
}

pub fn static_file_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        Files::new("/", "static")
            //路径下的默认文件
            .index_file("index.html")
            //如果找不到默认文件则打开静态资源根目录的index.html 可以解决游览器刷新后报错找不到文件的问题
            .default_handler(|req: ServiceRequest| async {
                let (req, _) = req.into_parts();
                let file = NamedFile::open_async("static/index.html").await?;
                let res = file.into_response(&req);
                Ok(ServiceResponse::new(req, res))
            }),
    );
}

pub fn resource_routes(cfg: &mut web::ServiceConfig) {
    cfg.route(
        "/api/resource/list",
        web::get().to(ResourceController::list),
    )
    .route(
        "/api/resource/page",
        web::post().to(ResourceController::page),
    )
    .route(
        "/api/resource/info/{id}",
        web::get().to(ResourceController::info),
    )
    .route("/api/resource/add", web::post().to(ResourceController::add))
    .route(
        "/api/resource/update",
        web::post().to(ResourceController::update),
    )
    .route(
        "/api/resource/delete/{id}",
        web::get().to(ResourceController::delete),
    )
    .route(
        "/api/resource/tree",
        web::get().to(ResourceController::resource_tree),
    );
}

pub fn auth_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(auth::get_captcha_v1)
        .service(auth::get_captcha_v2)
        .service(auth::register)
        .service(auth::login)
        .service(auth::logout)
        .service(auth::get_user_menu)
        .service(auth::user_info)
        .service(auth::reset_password)
        .service(auth::send_email)
        .service(auth::email_login);
}
